<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="custom" tagdir="/WEB-INF/tags" %>
<html>
<body>
<form:form commandName="formDto" id="form">
    <form:hidden path="caseUuid"/>
    <input type="hidden" name="employeeUuid" id="employeeUuid">

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            &times;
        </button>
        <h4 class="modal-title" id="myModalLabel">
            案件分派&nbsp;&nbsp;&nbsp;&nbsp;
            <small>带有<span class="fa fa-gears"></span> 标记的员工是该部门下具有分派权限的员工</small>
        </h4>
    </div>
    <div class="modal-body">
        <div class="form-group">
            <div class="col-sm-12">
                <div class="btn-group pull-right" role="group" style="margin-bottom: 0px;">
                    <c:if test="${not empty formDto.currentDept}">
                        <a href="#" class="btn btn-default btn-sm pull-right loadModal"
                           url="assignment?caseUuid=${formDto.caseUuid}&deptUuid=${formDto.currentDept.parentUuid}">返回</a>
                    </c:if>
                </div>
                <h4 class="panel-title">
                    <c:if test="${not empty formDto.currentDept}">
                        <code>${formDto.currentDept.name}</code>下级
                    </c:if>部门列表
                </h4>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-12">
                <c:forEach items="${formDto.groupDepts}" var="groupDept">
                    <c:forEach items="${groupDept.value}" var="dept">
                        <button type="button" style="width:30%" class="btn btn-primary deptItem loadModal"
                                url="assignment?caseUuid=${formDto.caseUuid}&deptUuid=${dept.uuid}">
                                ${dept.name}</button>
                    </c:forEach>
                    <br/>
                </c:forEach>
            </div>
        </div>
        <c:if test="${not empty formDto.employeeList}">
            <div class="form-group">
                <div class="col-sm-12"><h4 class="panel-title"><code>${formDto.currentDept.name}</code>员工列表</h4></div>
            </div>

            <div class="form-group">
                <div class="col-sm-12">
                    <c:forEach items="${formDto.employeeList}" var="emp">
                        <button type="button" class="btn btn-primary employeeItem"
                                uuid="${emp.uuid}">
                            <c:if test="${emp.leader}"><span class="fa fa-gears"></span></c:if>
                                ${emp.name}
                        </button>
                    </c:forEach><br/>
                    <span class="error text-danger ErrorText"></span>
                </div>
            </div>
        </c:if>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-primary" id="SubmitBtn">
            确定
        </button>
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭
        </button>
    </div>
</form:form>
<style type="text/css">
    .deptItem, .employeeItem {
        margin: 3px 0;
    }
</style>
<script type="text/javascript">
    $("#SubmitBtn").click(function () {
        var serialize = $("#form").serialize();
        $.ajax({
            url: 'assignment',
            data: serialize,
            type: 'post',
            cache: false,
            success: function (data) {
                if (data.state) {
                    window.location.reload();
                } else {
                    $(".ErrorText").html(data.msg);
                }
            },
            error: function () {
                alert("失败");
            }
        });
    });
    $(".employeeItem").click(function () {
        $(this).siblings().removeClass("btn-success");
        $(this).addClass("btn-success");
        $("#employeeUuid").val($(this).attr("uuid"));
    });
</script>
</body>
</html>
